package com.wandian.games.mvvm.gamebook;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.iBookStar.views.YmConfig;
import com.iBookStar.views.YmWebView;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.wandian.games.R;
import com.wandian.games.databinding.GamesFragmentBookBinding;

import me.jessyan.autosize.utils.ScreenUtils;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 小说fragment
 **/
@Route(path = RouterConfig.ROUTER_FRAGMENT_GAMES_BOOK)
public class GameBookFragment extends BaseMVVMFragment<GameBookFragmentViewModel> {
    private GamesFragmentBookBinding dataBinding;
    private FragmentManager mSupportFragmentManager;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.inflate(inflater, R.layout.games_fragment_book, container, false);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        dataBinding.setViewModel(mViewModel);
        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) dataBinding.viewSys.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
        linearParams.height = ScreenUtils.getStatusBarHeight();
        dataBinding.viewSys.setLayoutParams(linearParams);
        initViewData();

    }

    private void initViewData(){
      //  fragments.add(new ReaderFragment());
        mSupportFragmentManager = getActivity().getSupportFragmentManager();
//        mTransaction = mSupportFragmentManager.beginTransaction();
        mSupportFragmentManager.beginTransaction().replace(R.id.fragment_game_book, new ReaderFragment()).commitAllowingStateLoss();
    }


    //小说自定义Fragment
    public static class ReaderFragment extends Fragment {
        private YmWebView ymWebView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //配置落地页TitleBar的背景颜色和文字颜色
            YmConfig.setTitleBarColors(0xffffffff, 0xff000000);
            YmConfig.setOutUserId(AppContext.getInstance().getAppPreferencesHelper().getKeyValue("user_id"));
            //YmConfig.setOutUserId("12345678");			//对接金币必须事先调用此接口
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            if(ymWebView == null) {
                //初始化YmWebView，即可打开书城
                ymWebView = new YmWebView(getActivity());
                //出现容器和h5页面上事件冲突时，通知h5
                ymWebView.disableSwiperTouch();
                ymWebView.openBookStore();
            }
            return ymWebView;
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    }


}
